<?php

/**
 * XHTML_Ul, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Ol class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Common_Element.php";
include_once "XHTML__Lists.php";
include_once "XHTML_Li.php";


/**
 * The XHTML_Ol class is a handler for the ol xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Ol extends XHTML_Common_Element implements XHTML__Lists {
    
    // inner elements
    
    /**
     * XHTML_Li elements the ol element has in its content.
     *
     * @var array
     */
    protected $items;
    
    
    // Methods
    
    /**
     * Constructs a XHTML_Ol object with one item.
     * 
     * @param XHTML_Li $firstItem
     */
    function __construct(XHTML_Li $firstItem)
    {
        settype($this->elements, "array");
        array_push($this->items, $firstItem);
    }
 
    /**
     * Sets the XHTML_Li elements array.
     *
     * @param array
     */
    function setItems($items) {
        $this->items = $items;
    }
    
    /**
     * Gets the XHTML_Li elements array.
     *
     * @return array
     */
    function getItems()
    {
        return $this->items;
    }
    
    /**
     * Adds a XHTML_Li element to the inner array of this object.
     * 
     * @param XHTML_Li
     */
    function addElement(XHTML_Li $item)
    {
        array_push($this->items, $item);
    }
    
    /**
     * Removes a XHTML_Li element from the inner array of this object.
     * It will always keep one item in the array.
     * 
     * @param XHTML_Li
     */
    function removeElement(XHTML_Li $item)
    {
        if (count($this->items) > 1) {
            if (($res = array_search($item, $this->items)) !== false) {
                unset($this->items[$res]);
            }
        }
    }

    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<ol".parent::toString();
        
        $auxString = $auxString.">\n";
        
        /*
         * Additions of the inner items of the ol element to the return 
         * string.
         */
        if (count($this->items) > 0) {
            foreach ($this->items as $item) {
                $auxString = $auxString.$item->toString();
            }
        }
        
        $auxString = $auxString."</ol>\n";
        
        return $auxString;
    }
    
}

?>